/**
* @author AlbertDarren
* @contact 2563491540@qq.com
*/
#include <cstdio>

using namespace std;
struct Person
{
    char ID_number[16];
    int Sign_in_time,Sign_out_time;//total seconds
} unlocked,temp,locked;
void init()
{
    unlocked.Sign_in_time=locked.Sign_in_time=24*3600+1;
    unlocked.Sign_out_time=locked.Sign_out_time=-1;
}
int main_1006()
{
    init();
    int M,sh,sm,ss,eh,em,es;
    scanf("%d",&M);
    for (int i=0;i<M ; ++i)
    {
        scanf("%s %d:%d:%d %d:%d:%d",temp.ID_number,&sh,&sm,&ss,&eh,&em,&es);
        temp.Sign_in_time=3600*sh+60*sm+ss;
        temp.Sign_out_time=3600*eh+60*em+es;
        if (temp.Sign_in_time<unlocked.Sign_in_time)
        {
            unlocked=temp;
        }
        if (temp.Sign_out_time>locked.Sign_out_time)
        {
            locked=temp;
        }
    }
    printf("%s %s",unlocked.ID_number,locked.ID_number);
	return 0;
}
